package com.product.product.controller;

import com.product.product.domain.Product;
import com.product.product.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("api/v1/product")
@RestController
public class ProductController {

    @Value("${server.port}")
    String port;

    @Autowired
    public ProductService productService;

    /**
     * 获取所以商品列表
     * @return
     */
    @RequestMapping("list")
    public Object list(){
        return productService.listProduct();
    }

    /**
     * 根据ID查找商品详情
     * @param id
     * @return
     */
    @RequestMapping("find")
    public Object findById(@RequestParam("id") Integer id){
        Product product = productService.findById(id);
        Product result = new Product();
        BeanUtils.copyProperties(product, result);
        product.setName(result.getName() + "the client port  :  " + port);

        return result;
    }
}
